package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunPostLike;
import com.ruoyi.quanzi.service.ISunPostLikeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 帖子点赞Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/quanzi/like")
public class SunPostLikeController extends BaseController
{
    @Autowired
    private ISunPostLikeService sunPostLikeService;

    /**
     * 查询帖子点赞列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunPostLike sunPostLike)
    {
        startPage();
        List<SunPostLike> list = sunPostLikeService.selectSunPostLikeList(sunPostLike);
        return getDataTable(list);
    }

    /**
     * 导出帖子点赞列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:export')")
    @Log(title = "帖子点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunPostLike sunPostLike)
    {
        List<SunPostLike> list = sunPostLikeService.selectSunPostLikeList(sunPostLike);
        ExcelUtil<SunPostLike> util = new ExcelUtil<SunPostLike>(SunPostLike.class);
        util.exportExcel(response, list, "帖子点赞数据");
    }

    /**
     * 获取帖子点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunPostLikeService.selectSunPostLikeById(id));
    }

    /**
     * 新增帖子点赞
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:add')")
    @Log(title = "帖子点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunPostLike sunPostLike)
    {
        return toAjax(sunPostLikeService.insertSunPostLike(sunPostLike));
    }

    /**
     * 修改帖子点赞
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:edit')")
    @Log(title = "帖子点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunPostLike sunPostLike)
    {
        return toAjax(sunPostLikeService.updateSunPostLike(sunPostLike));
    }

    /**
     * 删除帖子点赞
     */
    @PreAuthorize("@ss.hasPermi('quanzi:like:remove')")
    @Log(title = "帖子点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunPostLikeService.deleteSunPostLikeByIds(ids));
    }

    /**
     * 用户点赞帖子
     */
    @PostMapping("/like/{postId}")
    public AjaxResult likePost(@PathVariable Long postId)
    {
        Long userId = getUserId();
        return toAjax(sunPostLikeService.likePost(userId, postId));
    }

    /**
     * 用户取消点赞帖子
     */
    @DeleteMapping("/unlike/{postId}")
    public AjaxResult unlikePost(@PathVariable Long postId)
    {
        Long userId = getUserId();
        return toAjax(sunPostLikeService.unlikePost(userId, postId));
    }

    /**
     * 获取帖子的点赞列表
     */
    @GetMapping("/post/{postId}")
    public TableDataInfo getPostLikes(@PathVariable Long postId)
    {
        startPage();
        List<SunPostLike> list = sunPostLikeService.selectLikesByPostId(postId);
        return getDataTable(list);
    }

    /**
     * 获取用户点赞的帖子列表
     */
    @GetMapping("/user/{userId}")
    public TableDataInfo getUserLikes(@PathVariable Long userId)
    {
        startPage();
        List<SunPostLike> list = sunPostLikeService.selectLikesByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 检查用户是否点赞了某个帖子
     */
    @GetMapping("/check/{postId}")
    public AjaxResult checkLike(@PathVariable Long postId)
    {
        Long userId = getUserId();
        boolean isLiked = sunPostLikeService.isPostLikedByUser(userId, postId);
        return success(isLiked);
    }

    /**
     * 获取帖子的点赞数量
     */
    @GetMapping("/count/{postId}")
    public AjaxResult getLikeCount(@PathVariable Long postId)
    {
        int count = sunPostLikeService.getLikeCountByPostId(postId);
        return success(count);
    }
}